new Vue({
    el: '#app',
    mixins: [Save],//组件配置文件，路径/utils/components.js
    data() {
        return {
            dataForm: {},
            type: '0',// 0:配置 1:预览
            childDataForm: [{
                id:createUniqueString(),
                Warehouse_Region_Name: '1区',
                Passageway_Number: '',
                Columns_Number: '',
                Rows_Number: '',
                Pallet_Number: '',
                Height_Number: '',
                Detail: [],
            }],
            activeName: '',
            treeData: [],//区域
            selectDetail: { Customize: [] },
            selectStorage: '',
            dialogVisible: false,
        }
    },
    mounted() {
        /**
         * 组件配置文件中自动执行方法
         * 1.this.init(); //初始化文件
         * 2.this.getData(); //仅修改时执行，根据BOM配置属性查询
         */
        this.activeName = this.childDataForm[0].id;
        let ls_URL = window.location.href;
        this.type = getQueryObject(ls_URL, "type") || '0';
        if (this.type == '1') {
            this.getList(this.nodeKey);
        }
    },
    computed: {
        Customize() {
            return function (item, y, x) {
                let obj = item.Customize.filter(i => `${i.Rows}-${i.Columns}` == `${y}-${x}`)[0];
                return obj ? obj : {};
            }
        },
        Preview() {
            let obj = this.childDataForm.filter(i => i.id == this.activeName)[0];
            return obj ? obj.Detail : [];
        }
    },
    methods: {
        async getList(nodeKey) { //预览
            const warehouse_region = await generalApi.generalQueryByTableNameWithOriginal({// 区域
                Data: 'warehouse_region',
                Condition: `Warehouse_SerialNumber='${nodeKey}'`,
            })
            this.treeData = warehouse_region;
            if (warehouse_region.length > 0) {
                this.activeName = warehouse_region[0].Warehouse_Region_SerialNumber;
                let childDataForm = [];zhao
                for (let i = 0; i < warehouse_region.length; i++) {
                    const item = warehouse_region[i];
                    const res = await generalApi.generalQueryByTableNameWithOriginal({// 
                        Data: 'warehouse_rules',
                        Condition: `Warehouse_Region_SerialNumber='${item.Warehouse_Region_SerialNumber}'`,
                    });
                    if (res.length > 0) {
                        const obj = res[0];
                        childDataForm.push({
                            id:obj.Warehouse_Region_SerialNumber,
                            Warehouse_Region_Name: obj.Warehouse_Region_Name,
                            Passageway_Number: obj.Passageway_Number,
                            Columns_Number: obj.Columns_Number,
                            Rows_Number: obj.Rows_Number,
                            Pallet_Number: obj.Pallet_Number,
                            Height_Number: obj.Height_Number,
                            Detail: JSON.parse(obj.Detail_JSON),
                        });
                    }
                }
                this.childDataForm = childDataForm;
                this.$refs.tree.setCurrentKey(warehouse_region[0].Warehouse_Region_SerialNumber);
            }
        },
        handleNodeClick() {

        },
        handleTabsEdit(targetName, action) {
            if (action === 'add') {
                this.childDataForm.push({
                    id:createUniqueString(),
                    Warehouse_Region_Name: (this.childDataForm.length + 1) + '区',
                    Passageway_Number: '',
                    Columns_Number: '',
                    Rows_Number: '',
                    Pallet_Number: '',
                    Height_Number: '',
                    Detail: [],
                });
            } else if (action === 'remove') {
                if (this.childDataForm.length > 1) {
                    this.childDataForm = this.childDataForm.filter(i => i.id != targetName);
                } else {
                    this.$message({
                        message: '不能在删除了',
                        type: 'warning'
                    })
                }
            }
        },
        changeConfig(item) {
            const { Passageway_Number, Columns_Number, Rows_Number, Pallet_Number, Height_Number } = item;
            item.Detail = [];
            if (Passageway_Number > 0 && Columns_Number > 0 && Rows_Number > 0 && Pallet_Number > 0 && Height_Number > 0) {
                for (let i = 0; i < Passageway_Number; i++) {
                    item.Detail.push({
                        id:createUniqueString(),
                        Passageway_Name: '第' + (i + 1) + '项道',
                        Storage_Type: '0',// 0: 左右 1:仅左 2:仅右
                        Left_Columns_Number: Columns_Number,
                        Left_Rows_Number: Rows_Number,
                        Right_Columns_Number: Columns_Number,
                        Right_Rows_Number: Rows_Number,
                        Pallet_Number: Pallet_Number,
                        Height_Number: Height_Number,
                        Customize: [],
                    })
                }
            }
        },
        handlePallet(item, y, x) {
            this.selectStorage = `${y}-${x}`;
            if (!item.Customize.some(i => `${i.Rows}-${i.Columns}` == this.selectStorage)) {
                item.Customize.push({
                    Columns: x,
                    Rows: y,
                    Pallet_Number: item.Pallet_Number,
                    Height_Number: item.Height_Number,
                });
            }
            this.selectDetail = item;
            this.dialogVisible = true;
        },
        async handleSubmit() {
            let warehouse_region = [], warehouse_rules = [];
            this.childDataForm.forEach((obj, id) => {
                let Warehouse_Region_SerialNumber = this.dataForm.Warehouse_SerialNumber + '-' + addZero(id + 1, 2)
                let warehouse_storage_location = [];
                obj.Detail.forEach((item, index) => {
                    if (item.Storage_Type == '0' || item.Storage_Type == '1') {
                        for (let y = 1; y <= item.Left_Rows_Number; y++) {
                            for (let x = 1; x <= item.Left_Columns_Number; x++) {
                                // 生成托盘
                                for (let z = 1; z <= (Number(this.Customize(item, y, 2 * x - 1).Pallet_Number) || Number(item.Pallet_Number)); z++) {
                                    warehouse_storage_location.push({
                                        Warehouse_Storage_Location_SerialNumber: `${Warehouse_Region_SerialNumber}-${index + 1}-${addZero(y, 2)}${addZero(2 * x - 1, 2)}-${z}`,
                                        Warehouse_Region_SerialNumber,
                                        Warehouse_Storage_Location_Code: `${index + 1}-${addZero(y, 2)}${addZero(2 * x - 1, 2)}-${z}`,
                                        Height_Number: this.Customize(item, y, 2 * x - 1).Height_Number || item.Height_Number,
                                        Enterprise_Info_SerialNumber: getCookie('enterprise'),
                                    })
                                }

                            }
                        }
                    }
                    if (item.Storage_Type == '0' || item.Storage_Type == '2') {
                        for (let y = 1; y <= item.Left_Rows_Number; y++) {
                            for (let x = 1; x <= item.Left_Columns_Number; x++) {
                                // 生成托盘
                                for (let z = 1; z <= (Number(this.Customize(item, y, 2 * x).Pallet_Number) || Number(item.Pallet_Number)); z++) {
                                    warehouse_storage_location.push({
                                        Warehouse_Storage_Location_SerialNumber: `${Warehouse_Region_SerialNumber}-${index + 1}-${addZero(y, 2)}${addZero(2 * x, 2)}-${z}`,
                                        Warehouse_Region_SerialNumber,
                                        Warehouse_Storage_Location_Code: `${index + 1}-${addZero(y, 2)}${addZero(2 * x - 1, 2)}-${z}`,
                                        Height_Number: this.Customize(item, y, 2 * x - 1).Height_Number || item.Height_Number,
                                        Enterprise_Info_SerialNumber: getCookie('enterprise'),
                                    })
                                }

                            }
                        }
                    }
                })
                warehouse_region.push({
                    Warehouse_Region_SerialNumber,
                    Warehouse_Region_Name: obj.Warehouse_Region_Name,
                    Warehouse_SerialNumber: this.dataForm.Warehouse_SerialNumber,
                    Enterprise_Info_SerialNumber: getCookie('enterprise'),
                    Children: {
                        warehouse_storage_location,
                        warehouse_rules: [{
                            Warehouse_Region_SerialNumber,
                            Warehouse_Region_Name: obj.Warehouse_Region_Name,
                            Passageway_Number: obj.Passageway_Number,
                            Columns_Number: obj.Columns_Number,
                            Rows_Number: obj.Rows_Number,
                            Pallet_Number: obj.Pallet_Number,
                            Height_Number: obj.Height_Number,
                            Detail_JSON: JSON.stringify(obj.Detail),
                            Enterprise_Info_SerialNumber: getCookie('enterprise'),
                        }]
                    }
                })
            })
            const lJSONObject_DataSet = {
                Add: { warehouse_region }
            }

            // let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
            // lJSONObject_DataSet['Add']['base_operation_log'] = [{
            //     Operation_Type: '0',
            //     Operation_Path: getRelativePath(),
            //     Operation_Description: '保存（新增/修改）数据',
            //     Operation_Main,
            //     Operation_Interface: "generalSaveByRequestBody",
            //     Operation_IP: getIpAddress()
            // }]

            // 提交配置
            try {
                const res = await generalApi.generalSaveByRequestBody({
                    Data: lJSONObject_DataSet
                })

                if (res.Status == "success") {
                    this.$message.success("提交成功");
                    window.location.href = './index.html';
                } else {
                    this.$message.error("提交失败，请重试！");
                }

            } catch (error) {
                console.error(error);
                this.$message.error("系统错误，请联系开发者!")
                this.loading = false;
            }
            // this.loading = false;
        }
    }
})